package Json_demo;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class Json_read_ListOBJ {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//1 khai bao su dung thu vien cua Json
		ObjectMapper objma = new ObjectMapper();
//2 bat cua so chon files
		JFileChooser objFile = new JFileChooser();
        objFile.setCurrentDirectory(new java.io.File("."));
        objFile.setDialogTitle("Chọn file: ");
        objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        String[] EXTENSION = new String[] { "json" };
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JSON files", EXTENSION);
        objFile.setFileFilter(filter);
        objFile.setMultiSelectionEnabled(false);

        int returnVal = objFile.showOpenDialog(null);
        String ParentPath = "";
        String FileName = "";
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            // ParentPath = objFile.getSelectedFile().getParent();
            FileName = objFile.getSelectedFile().getPath();
// khoi tao ra 1 list de luu tru du lieu
            List<Teacher> ListKQ = new ArrayList<Teacher>();
            try {
				ListKQ = objma.readValue(new File(FileName), new TypeReference<List<Teacher>>() {});
			for(int i = 0; i<ListKQ.size();i++){
				System.out.println("Teacher"+i);
				System.out.println("RollNo :"+ListKQ.get(i).getRollNo());
				System.out.println("FullName :"+ListKQ.get(i).getFullName());
				System.out.println("Mon day :"+ListKQ.get(i).getMon());
				System.out.println("Diem so :"+ListKQ.get(i).getDiemSo());
			}
			System.out.println("vay la ban da co 1 list chua nhieu teacher !");
			} catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	}
}
